/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.rpc.clients;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import uvexposureapplication.client.UVAppUI;
import uvexposureapplication.rpc.AbstractPseudoRPCClientStub;

/**
 *
 * @author matthieukluj
 */
public class ContextManagerPseudoRPCClientStub extends AbstractPseudoRPCClientStub {

    UVAppUI uVAppUI;

    public ContextManagerPseudoRPCClientStub(UVAppUI uVAppUI, String methodName,
            String parameters, String server) {
        super(methodName, parameters, server);
        this.serverName = "ContextManager";
        this.uVAppUI = uVAppUI;
    }

    @Override
    public void produce() {
        Map<String, String> notification = new HashMap<String, String>();
        notification.put("RPCServerName", this.serverName);
        notification.put("MethodName", this.methodName);
        notification.put("Parameters", this.parameters);
        notification.put("Side", "Client");
        this.producer.produceFromCollection(notification);
        synchronized (this) {
            try {
                this.wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(LocationServerPseudoRPCClientStub.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void subscribeToServer() {
        this.consumer.subscribe("RPCServerName == '" + this.serverName + "' "
                + "&& MethodName == '" + this.methodName + "'");

    }

    @Override
    public void update(Observable o, Object arg) {

        if (arg instanceof Map) {
            Map<String, String> argMap = (Map<String, String>) arg;

            if (argMap.get("RPCServerName").equals(this.serverName)) {
                if (argMap.get("Side").equals("Server")) {
                    if (argMap.get("MethodName").equals("searchItemsInLocation")) {
                        synchronized (this) {
                            this.notify();
                            this.uVAppUI.printItemInLocation((String) argMap.get("Result"));
                        }
                    } else if (argMap.get("MethodName").equals("searchInfos")) {
                        synchronized (this) {
                            this.notify();
                            this.uVAppUI.printItemInformation((String) argMap.get("Result"));
                        }
                    } else if (argMap.get("MethodName").equals("shutDown")){
                       synchronized (this) {
                           this.notify();
                       }
                    }
                }
            }
        }
    }
}
